#! /bin/bash

curl -G 'http://10.202.94.141:30002/api/v1/query' --data-urlencode 'query=kube_pod_container_status_restarts_total{namespace!="kube-system",namespace!~"oms4.*",namespace!="cattle-system",namespace!="rook-ceph",namespace!="logging",namespace!="monitoring",namespace!="website-qc",namespace!="cattle-fleet-system",namespace!="kubernetes-dashboard",namespace!="public-service"}>0'|jq -r '.data.result[].metric | "\(.namespace),\(.pod),\(.container)"'>/tmp/mon_pod

lova=("cms" "sto" "snapshot" "tms-agent" "oms" "lova" "oact" "vsp")
cs=("aas" "auth-gateway" "sch" "dbm" "ftmq" "fmp" "sysmds" "oss" "b2b" "dts" "locale" "print" "mdc" "mds" "cxs" "bps")

lovazz=$(IFS=','; echo "${lova[*]}")
cszz=$(IFS=','; echo "${cs[*]}")

for i in `cat /tmp/mon_pod`
do
  nsname=`echo $i|awk -F',' '{print $1}'`
  podname=`echo $i|awk -F',' '{print $2}'`
  containername=`echo $i|awk -F',' '{print $3}'`
  kubectl describe pod $podname -n $nsname|grep OOMKilled
  #echo "pod="$podname
  if [ $? -eq 0 ];then
    if [[ $containername == "ms-"* ]];then
      echo "fse"
      echo $nsname-$podname-$containername
      ssh -o "StrictHostKeyChecking no" root@10.202.16.154 "echo "The ${podname} service in ${nsname} environment Service experienced an OOM exception !"|mail -s Microservice\ out\ of\ memory\ alarm  zhang.zhe@y3.sg,jiannluen.kow@y3.sg"
    elif [[ ",$lovazz," == *",$containername,"* ]];then
      echo "lova"
      echo $nsname-$podname-$containername
    ssh -o "StrictHostKeyChecking no" root@10.202.16.154 "echo "The ${podname} service in ${nsname} environment Service experienced an OOM exception !"|mail -s Microservice\ out\ of\ memory\ alarm  zhang.zhe@y3.sg,yi.he@y3.sg"
    elif [[ ",$cszz," == *",$containername,"* ]];then
      echo "cs"
      echo $nsname-$podname-$containername
    ssh -o "StrictHostKeyChecking no" root@10.202.16.154 "echo "The ${podname} service in ${nsname} environment Service experienced an OOM exception !"|mail -s Microservice\ out\ of\ memory\ alarm  zhang.zhe@y3.sg,dingkang.hu@y3.sg,guocheng.liang@y3.sg"
    fi 
  fi
done
